//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by 许克辉 on 16/4/24.
//  Copyright © 2016年 xukehui. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //点击事件
     @objc private func composeDidClik(){
        print(__FUNCTION__)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //由于tabbar是只读 不能够直接操作，如果要修改 可以使用KVC
        let mainTabBar = MainTabBar()
        //KVC赋值
        setValue(mainTabBar, forKey: "tabBar")
        
 
        //添加子视图控制器
        addChildViewControllers()
        //打印函数的类名
        print(tabBar.classForCoder)
        
        //设置点击事件
        mainTabBar.composeBtn.addTarget(self, action: "composeDidClik", forControlEvents: .TouchUpInside)
    }

    private func addChildViewControllers() {
        addChildViewCintrollers(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewCintrollers(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewCintrollers(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewCintrollers(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")

            }
    
     //重载
    private func addChildViewCintrollers(vc: UIViewController,title: String, imageName: String){
       
        self.tabBar.tintColor = UIColor.orangeColor() //和后面注释掉的代码作用相同
        
        //实例化导航视图控制器
        let nav = UINavigationController(rootViewController: vc)
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
//        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        
//        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
//        
        addChildViewController(nav)//添加nav

    }

   }
